﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using Hi_Baidu.ProcessModel;

namespace Hi_Baidu.Timer
{
    /// <summary>
    ///      提供了更新用户上线状态的超时计算和操作
    /// </summary>
    class ChanageUserOnlineState_TimeOutEvent : TimeOutEvent
    {
        private byte[] username;
        private byte[] OnlineState;
        /// <summary>
        ///      提供了更新用户上线状态的超时计算和操作
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="OnlineState">上线状态</param>
        /// <param name="Key">唯一标示</param>
        /// <param name="Interval">触发间隔</param>
        /// <param name="TryCount">重试次数</param>
        public ChanageUserOnlineState_TimeOutEvent(byte[] username, byte[] OnlineState, String Key, int Interval, int TryCount) : base(Key, Interval, TryCount)
        {
            this.username = username;
            this.OnlineState = OnlineState;
            this.OnTryFull += new EventHandler<EventArgs>(ChanageUserOnlineState_TimeOutEvent_OnTryFull);
            this.OnTick += new EventHandler<EventArgs>(ChanageUserOnlineState_TimeOutEvent_OnTick);
        }

        void ChanageUserOnlineState_TimeOutEvent_OnTick(object sender, EventArgs e)
        {
            //重试发送更新用户状态请求
            UserProcessModel.ChanageUserOnlineState(this.username, this.OnlineState);
        }

        void ChanageUserOnlineState_TimeOutEvent_OnTryFull(object sender, EventArgs e)
        {
            //还原用户上线状态
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(UserProcessModel.ReSetUserOnlineStateToOrg));
            //提示错误
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(Globle.ShowWindow));
        }
    }
}
